<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="页面名称" prop="pageName">
        <el-input
          v-model="queryParams.pageName"
          placeholder="请输入页面名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-checkbox v-model="isType" style="margin-left: 20px;">{{isType?'列表':'卡片'}}</el-checkbox>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ghxx:bEchartZt:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ghxx:bEchartZt:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ghxx:bEchartZt:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bEchartZtList" @selection-change="handleSelectionChange" v-if="isType">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="页面名称" align="center" prop="pageName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleClickZt(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览</el-button>  
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ghxx:bEchartZt:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row v-loading="loading" v-else>
      <el-col :span="4" v-for="item in bEchartZtList" :key="item.id" style="padding: 2px;">
        <div style="width:auto;height:300px;">
          <el-card :body-style="{ padding: '5px'}">
            <span class="device-name">{{ item.pageName }}</span>
            <div @click="handlePreview(item)">
              <el-image :src="item.pageImage == null ? noImage : baseApi + item.pageImage" title="编辑" class="ztImage"
              />
            </div>
            <div style="padding: 5px;margin-top: 5px;">
              <span>{{ item.pageName }}</span>
              <div class="bottom clearfix">
                <el-button type="text" @click="handleDelete(item)" icon="el-icon-delete">删除</el-button>
                <el-button type="text" @click="handleClickZt(item)" icon="el-icon-edit">编辑</el-button>
                <el-button type="text" icon="el-icon-view" @click="handlePreview(item)">预览</el-button>  
              </div>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大屏管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="页面名称" prop="pageName">
          <el-input v-model="form.pageName" placeholder="请输入页面名称" />
        </el-form-item>
        <el-form-item label="页面图片">
          <image-upload v-model="form.pageImage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBEchartZt, getBEchartZt, delBEchartZt, addBEchartZt, updateBEchartZt } from "@/api/ghxx/bEchartZt";
import { getToken } from "@/utils/auth";
import noImage from '@/assets/image/no-echart.jpg'
export default {
  name: "BEchartZt",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大屏管理表格数据
      bEchartZtList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn:"id",
        isAsc:"desc",
        guid: null,
        deptIdStrs: null,
        pageName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      isAdmin:false,
      isType:false,
      baseApi: process.env.VUE_APP_BASE_API,
      noImage: noImage,
    };
  },
  created() {
    this.isAdmin=this.$auth.hasRole('admin');
    this.getList();
  },
  methods: {
    //预览
    handlePreview(row) {
      window.open('http://' + this.tipUtil.getServerIp() + ':' + this.tipUtil.getServerPort() + '/screenFull?guid=' + row.guid)
    },
    //编辑
    handleClickZt(row) {
      if (this.isAdmin) {
        window.open('http://' + this.tipUtil.getServerIp() + ':' + this.tipUtil.getServerPort() + '/screenEditor?guid=' + row.guid +'&accessToken=' + getToken())
      } else {
        window.open('http://' + this.tipUtil.getServerIp() + ':' + this.tipUtil.getServerPort() + '/screenEditor?guid=' + row.guid)
      }
    },
    /** 查询大屏管理列表 */
    getList() {
      this.loading = true;
      listBEchartZt(this.queryParams).then(response => {
        this.bEchartZtList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        guid: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptIdStrs: null,
        pageName: null,
        pageImage: null,
        pageData: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大屏管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBEchartZt(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改大屏管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBEchartZt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBEchartZt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除大屏管理编号为"' + ids + '"的数据项？').then(function() {
        return delBEchartZt(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ghxx/bEchartZt/export', {
        ...this.queryParams
      }, `大屏_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.ztImage {
  width: 100%;
  display: block;
  height: 200px;
}

.ztImage:hover {
  cursor: pointer;
  transform: scale(1.1)
}
.device-name {
    background-color: #5479f7;
    display: inline-block;
    color: #fff;
    text-align: center;
    height: 25px;
    line-height: 15px;
    font-size: 12px;
    padding: 4px 6px;
    position: fixed;
    margin: -5px;
    z-index: 2;
}
.bottom {
  margin-top: 13px;
  line-height: 12px;
  text-align: right;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>